package com.ygqh.baby.ao;

import java.io.Serializable;

/**
 * api接口返回对象
 *
 * @param
 * @author Administrator
 */
public class ResponseEntity implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private String code;

    private String msg;

    private Object data;

    public ResponseEntity(CodeConstant success) {
        super();
        this.code = CodeConstant.Success.getCode();
        this.msg = CodeConstant.Success.getTitle();
        this.data = null;
    }

    public ResponseEntity(CodeConstant codeConstant, Object data) {
        super();
        this.code = CodeConstant.Success.getCode();
        this.msg = CodeConstant.Success.getTitle();
        this.data = data;
    }

    public ResponseEntity() {
        super();
    }

    public ResponseEntity(String code, String msg, Object data) {
        super();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ResponseEntity converter(Message msg) {
        String code = "0000".equals(msg.getCode()) ? CodeConstant.Success.getCode() : msg.getCode();
        return new ResponseEntity(code, msg.getContent(), msg.getExtra());
    }

    public Message converter() {
        if (isSuccess()) {
            return Message.success(getData());
        }
        return Message.error(getMsg(), getData());
    }


    public static ResponseEntity success(Object data) {
        return new ResponseEntity(CodeConstant.Success.getCode(), CodeConstant.Success.getTitle(), data);
    }

    public static ResponseEntity success() {
        return new ResponseEntity(CodeConstant.Success.getCode(), CodeConstant.Success.getTitle(), null);
    }

    public static ResponseEntity error(Object data) {
        if (data instanceof CodeConstant) {
            CodeConstant code = (CodeConstant) data;
            return new ResponseEntity(code.getCode(), code.getTitle(), null);
        }
        return new ResponseEntity(CodeConstant.Error.getCode(), CodeConstant.Error.getTitle(), data);
    }

    public static ResponseEntity error() {
        return new ResponseEntity(CodeConstant.Error.getCode(), CodeConstant.Error.getTitle(), null);
    }

    public static ResponseEntity error(String code, String msg, Object data) {
        return new ResponseEntity(code, msg, data);
    }

    public static ResponseEntity error(String msg, Object data) {
        return new ResponseEntity("400", msg, data);
    }

    public static ResponseEntity error404() {
        return new ResponseEntity(CodeConstant.Error404.getCode(), CodeConstant.Error404.getTitle(), null);
    }

    public static ResponseEntity errorStatus(String data) {
        return new ResponseEntity(CodeConstant.ErrorStatus.getCode(), CodeConstant.ErrorStatus.getTitle(), data);
    }

    public static ResponseEntity paramNotNull(String data) {
        return new ResponseEntity(CodeConstant.ParamNotNull.getCode(), CodeConstant.ParamNotNull.getTitle(), data);
    }

    public Boolean isSuccess() {
        if (CodeConstant.Success.getCode().equals(code)) {
            return true;
        }
        return false;
    }

    public String getErrorMsg() {
        if ("200".equals(code)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        sb.append(msg).append(">>>").append(data == null ? "" : data.toString());
        return sb.toString();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

}
